package com.bsi.core.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Author: HuangSong
 * @Date: Created in 2020/1/3
 * 上下文装配Bean
 */
@Component
public class SpringContextUtils implements ApplicationContextAware{

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext app) throws BeansException {
        this.applicationContext = app;
    }

    //通过name获取Bean
    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> tclass){
        return applicationContext.getBean(tclass);
    }

    //通过name和class返回指定的Bean
    public static <T> T getBean(String name, Class<T> tClass){
        return applicationContext.getBean(name,tClass);
    }


}
